package com.hm.manage.controller;

import java.util.Arrays;
import java.util.List;

import com.hm.common.utils.DictUtils;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hm.common.annotation.Log;
import com.hm.common.core.controller.BaseController;
import com.hm.common.core.domain.AjaxResult;
import com.hm.common.enums.BusinessType;
import com.hm.manage.domain.Localpic;
import com.hm.manage.service.ILocalpicService;
import com.hm.common.utils.poi.ExcelUtil;
import com.hm.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 本地图片Controller
 * 
 * @author hm
 * &#064;date  2025-03-24
 */
@Slf4j
@RestController
@RequestMapping("/manage/localpic")
public class LocalpicController extends BaseController
{
    @Autowired
    private ILocalpicService localpicService;

    /**
     * 查询本地图片列表
     */
    @PreAuthorize("@ss.hasPermi('manage:localpic:list')")
    @GetMapping("/list")
    public TableDataInfo list(Localpic localpic)
    {
        startPage();
        List<Localpic> list = localpicService.selectLocalpicList(localpic);
        return getDataTable(list);
    }

    /**
     * 导出本地图片列表
     */
    @PreAuthorize("@ss.hasPermi('manage:localpic:export')")
    @Log(title = "本地图片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Localpic localpic)
    {
        List<Localpic> list = localpicService.selectLocalpicList(localpic);
        ExcelUtil<Localpic> util = new ExcelUtil<Localpic>(Localpic.class);
        // 把list里面的数据字典替换为字典
        List<Localpic> listed = localpicService.generateExportData(list);
        util.exportExcel(response, listed, "本地图片数据");
    }

    /**
     * 获取本地图片详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:localpic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(localpicService.selectLocalpicById(id));
    }

    /**
     * 新增本地图片
     */
    @PreAuthorize("@ss.hasPermi('manage:localpic:add')")
    @Log(title = "本地图片", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Localpic localpic)
    {
        return toAjax(localpicService.insertLocalpic(localpic));
    }

    /**
     * 修改本地图片
     */
    @PreAuthorize("@ss.hasPermi('manage:localpic:edit')")
    @Log(title = "本地图片", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Localpic localpic)
    {
        return toAjax(localpicService.updateLocalpic(localpic));
    }

    /**
     * 删除本地图片
     */
    @PreAuthorize("@ss.hasPermi('manage:localpic:remove')")
    @Log(title = "本地图片", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(localpicService.deleteLocalpicByIds(ids));
    }

    /**
     * 推送数据到kafka
     */
    @PreAuthorize("@ss.hasPermi('manage:localpic:list')")
    @Log(title = "本地图片", businessType = BusinessType.UPDATE)
    @PostMapping("/pushDataToKafka/{ids}")
    public AjaxResult pushDataToKafka(@PathVariable Long[] ids){
        log.info("ids:{}", Arrays.toString(ids));
        int i = localpicService.sendtokafka(ids);
        return toAjax(i);
    }

    /**
     * 推送数据到kafka
     * 搜索条件推送
     */
    @PreAuthorize("@ss.hasPermi('manage:localpic:list')")
    @Log(title = "本地图片", businessType = BusinessType.UPDATE)
    @PostMapping("/pushDataToKafkaBuQuery")
    public AjaxResult pushDataToKafkaByQuery(@RequestBody Localpic localpic){
        log.info("-----------------{}",localpic.toString());
        // 根据条件搜索ids
        List<Localpic> localpics = localpicService.selectLocalpicList(localpic);
        Long[] ids = new Long[localpics.size()];
        for (int i = 0; i<localpics.size();i++){
            ids[i] = localpics.get(i).getId();
        }
        log.info("------------pushDataToKafkaBuQuery-----------{}",localpic.getPicType());
        int sendtokafka = localpicService.sendtokafka(ids);
        return success(sendtokafka);
    }

    /***
     * 上传数据
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('manage:localpic:add')")
    @Log(title = "本地图片", businessType = BusinessType.INSERT)
    @PostMapping("upload")
    public  AjaxResult ImportExcelFile(MultipartFile file) throws Exception{
        ExcelUtil<Localpic> excelUtil = new ExcelUtil<>(Localpic.class);
        List<Localpic> localpics = excelUtil.importExcel(file.getInputStream());
        log.info("------/manage/localpic/upload----获取到的数据行数：{}条----",localpics.size());
        for (Localpic localpic:localpics) {
            // 校验数据是否为空
            if (localpic.getPic().isEmpty() || localpic.getPicType().isEmpty()) {
                return error("图片链接和图片类型不能为空！请重新上传！");
            }
            // 校验数据字段是否存在
            if (DictUtils.getDictValue("manage_pic_type",localpic.getPicType()).isEmpty()) {
                return error("数据字典项不存在，请检查文档！找不到字典值："+localpic.getPicType());
            }
        }

        for (Localpic localpic:localpics) {
            // 替换图片类型为字典值
            String picTypeValue = DictUtils.getDictValue("manage_pic_type",localpic.getPicType());
            log.info("---------------picTypeValue:{}-----",picTypeValue);
            localpic.setPicType(picTypeValue);
            localpicService.insertLocalpic(localpic);
        }

        return success("插入成功！插入"+localpics.size()+"条数据");
    }

}
